<template>
	<view>
		<u-navbar
			title="优惠券"
			:autoBack="true"
			placeholder
		>
		</u-navbar>
		<view style="padding: 0 16rpx;background-color: #fff;">
			<u-tabs :current="current" :list="dictList" @click="tabClick"  :lineColor="appColor" :activeStyle="{color: appColor}" :inactiveStyle="{color: '#333'}"></u-tabs>
		</view>
		<scroll-view @scrolltolower="onLower" :scroll-y="true" :style="{height: scrollViewHeight}">
			<template v-if="loading">
				<view style="padding: 32rpx;">
					<u-skeleton :avatarSize="0" style="margin-bottom: 32rpx;" :loading="loading" avatar rows="3" v-for="i in 8" :key="i"></u-skeleton>
				</view>
			</template>
			<template v-else-if="!loading && list.length === 0">
				<u-empty
				    mode="coupon"
					icon="http://cdn.uviewui.com/uview/empty/coupon.png"
				>
				</u-empty>
			</template>
			<template v-else-if="!loading && list.length > 0">
				<view style="padding: 32rpx;">
					<view class="coupon-item" v-for="item in list" :key="item.id">
						<view class="coupon-item-l-box">
							<view class="coupon-item-l">
								<view class="coupon-item-l-tag">{{item.category_dictText}}</view>
								<view class="coupon-item-l-content">
									<text :style="{color: appColor, fontSize: '48rpx',fontWeight: 'bold'}">&yen;{{item.discount}}</text>
									<text :style="{color: appColor, fontSize: '26rpx'}">满{{item.lowCashPrice}}元可用</text>
								</view>
							</view>
							<view class="coupon-item-m">
								<text style="font-size: 32rpx;color: #333; font-weight: bold;">{{item.couponName}}</text>
								<view style="height: 32rpx;"/>
								<text style="font-size: 26rpx;color: #666; font-weight: bold;" v-if="item.expiryType === '1'">{{Number(item.expiryDay) === 0 ? '长期有效' : `${item.endTime}到期`}}</text>
								<text style="font-size: 26rpx;color: #666; font-weight: bold;" v-else>{{item.startTime}}至{{item.endTime}}有效</text>
							</view>
						</view>
						<view @click="toGet(item)" class="coupon-item-r" :style="{backgroundColor: item.allowUse ? appColor : '#868686'}">
							<template v-if="item.allowUse && item.category === '4'">
								<view>展示</view>
								<view>二维码</view>
							</template>
							<template v-else-if="item.allowUse && item.category !== '4'">
								<view>立即</view>
								<view>使用</view>
							</template>
							<template v-else>
								<view>不可</view>
								<view>使用</view>
							</template>
						</view>
					</view>
				</view>
				<u-loadmore :status="status" />
			</template>
		</scroll-view>
	</view>
</template>

<script>
	import {myReceive, getDictItems} from '@/api/me.js';
	import {mapActions} from 'vuex';
	export default {
		data() {
			return {
				pageSize: 8,
				pageNo: 1,
				list: [], // 优惠券列表
				dictList: [
					{name: '全部', value: '-1'}
				], // 字典列表
				loading: true,
				status: 'loadmore',
				current: 0,
			}
		},
		computed: {
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 - 44 + 'px';
			}
		},
		onLoad() {
			// 获取字典表
			this.loading = true;
			getDictItems('market_member_coupon_status').then(res => {
				this.dictList = this.dictList.concat(res.result);
				this.getList();
			})
		},
		methods: {
			...mapActions(['setActiveTabbar']),
			// 领取优惠券
			toGet({id, allowUse, category}) {
				if(allowUse && category === '4') {
					let url = `/pages/me/couponCode?id=${id}`;
					this.$cuNavigateTo(url);
				} else if(allowUse && category !== '4') {
					this.setActiveTabbar({activeTabbar: 'home'});
					this.$cuNavigateTo('/pages/index/index');
				}
			},
			tabClick({index}) {
				this.current = index;
				this.pageNo = 1;
				this.getList();
			},
			// 优惠券列表
			getList() {
				if(this.pageNo === 1) {
					this.loading = true;
				}
				let status = this.dictList[this.current].value
				let postObj = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					status
				}
				if(this.current === 0) {
					delete postObj.status
				}
				myReceive(postObj).then(res => {
					const {current, pages, records} = res.result;
					if(this.pageNo === 1) {
						let empty = [];
						this.list = empty.concat(records);
					} else {
						this.list = this.list.concat(records);
					}
					if(current >= pages) {
						this.status = 'nomore'
					} else {
						this.pageNo ++;
						this.status = 'loading'
					}
				}).finally(() => {
					this.loading = false;
				})
			},
			onLower() {
				if(this.status === 'loading') {
					this.getList();
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.coupon-item {
		border-radius: 16rpx;
		width: 100%;
		height: 200rpx;
		-webkit-mask-image: radial-gradient(circle at right 54px bottom 8px, transparent 8px, red 8.5px), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);
		-webkit-mask-size: 100%, 4px 12px;
		-webkit-mask-repeat: repeat, repeat-y;
		-webkit-mask-position: 0 8px, calc(100% - 52px);
		-webkit-mask-composite: source-out;
		mask-composite: subtract;
		background-color: #fff;
		position: relative;
		overflow: hidden;
		margin-bottom: 32rpx;
		.coupon-item-l-box {
			padding-top: 16rpx;
			height: calc(100% - 32rpx);
			width: calc(100% - 52px);
			display: flex;
			.coupon-item-l {
				width: 200rpx;
				height: 100%;
				border-right: 1rpx dashed #f2f2f2;
				position: relative;
				.coupon-item-l-tag {
					left: 0;
					top: -16rpx;
					position: absolute;
					height: 36rpx;
					line-height: 36rpx;
					padding: 4rpx 8rpx;
					border-radius: 0 0 16rpx 0;
					background-color: red;
					color: #fff;
					font-size: 24rpx;
				}
				.coupon-item-l-content {
					width: 200rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
				}
			}
			.coupon-item-m {
				width: calc(100% - 200rpx);
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				padding: 0 16rpx;
			}
		}
		.coupon-item-r {
			position: absolute;
			width: 52px;
			height: 100%;
			right: 0;
			top: 0;
			text-align: center;
			font-size: 32rpx;
			color: #fff;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		}
	}
</style>